﻿using Core;
using Core.Data;
using Core.Domain.Directory;
using Core.Domain.Stores;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.Directory
{
    /// <summary>
    /// 省份服务实现类
    /// </summary>

    public partial class StateProvinceService : IStateProvinceService
    {
        #region Fields

        private readonly IRepository<StateProvince> _stateProvinceRepository;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public StateProvinceService(
            IRepository<StateProvince> stateProvinceRepository)
        {
            _stateProvinceRepository = stateProvinceRepository;
        }

        #endregion

        #region Methods
        /// <summary>
        /// 删除省份
        /// </summary>
        public virtual void DeleteStateProvince(StateProvince stateProvince)
        {
            if (stateProvince == null)
                throw new ArgumentNullException("stateProvince");

            _stateProvinceRepository.Delete(stateProvince);
        }

        /// <summary>
        /// 根据主键获取省份
        /// </summary>
        public virtual StateProvince GetStateProvinceById(int stateProvinceId)
        {
            if (stateProvinceId == 0)
                return null;

            return _stateProvinceRepository.GetById(stateProvinceId);
        }


        /// <summary>
        /// 获取所有省份
        /// </summary>
        public virtual IList<StateProvince> GetStateProvinces()
        {
            var query = from sp in _stateProvinceRepository.Table
                        orderby sp.DisplayOrder
                        select sp;
            var stateProvinces = query.ToList();
            return stateProvinces;
        }

        /// <summary>
        /// 获取所有省份
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public virtual IPagedList<StateProvince> GetStateProvincesByCountryId(string name = "", int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _stateProvinceRepository.Table;
            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(e => e.Name.Contains(name));

            query = query.OrderBy(e => e.DisplayOrder);

            return new PagedList<StateProvince>(query, pageIndex, pageSize);
        }

        /// <summary>
        /// 新增省份
        /// </summary>
        public virtual void InsertStateProvince(StateProvince stateProvince)
        {
            if (stateProvince == null)
                throw new ArgumentNullException("stateProvince");

            _stateProvinceRepository.Insert(stateProvince);
        }

        /// <summary>
        /// 更新省份
        /// </summary>
        public virtual void UpdateStateProvince(StateProvince stateProvince)
        {
            if (stateProvince == null)
                throw new ArgumentNullException("stateProvince");

            _stateProvinceRepository.Update(stateProvince);
        }

        #endregion
    }
}
